//
//  WenZhangXunLianMainViewController.swift
//  CKC
//  Created by 张振东 on 16/10/2.
//  Copyright © 2016年 ZZD. All rights reserved.
//

import UIKit

class WenZhangXunLianMainViewController: UIViewController,UITextViewDelegate {

    //获取模拟器或者真机的宽度和高度
    let screenWidth = UIScreen.main.bounds.width
    let screenHeight = UIScreen.main.bounds.height
    
    //定义一个变量,用于存储模式类别
    var moShiLeiBie:String?
    
    //定义一个变量,用于存储传递过来的分钟值
    var fenZhongZhi:String?
    
    //定义一个变量,用于存储传递过来的词组模式值
    var wenZhangMoShi:String?
    
    //定义scrollView对象
    var scrollView:UIScrollView?
    
    //定义三个ImageView
    var zanTingImageView:UIImageView?
    var chaKanJieGuoImageView:UIImageView?
    var jieShuImageView:UIImageView?
    
    //定义两个View对象
    var firstFenGeXianView:UIView?
    var secondFenGeXianView:UIView?
    
    //定义三个按钮
    var zanTingBtn:UIButton?
    var chaKanJieGuoBtn:UIButton?
    var jieShuBtn:UIButton?
    
    //定义一条横线UIView
    var hengXianView:UIView?
    
    //创建8个UILabel对象
    var zhengQueShuLabel:UILabel?
    var zhengQueShuLabelValue:UILabel?
    
    var zhengQueLvLabel:UILabel?
    var zhengQueLvLabelValue:UILabel?
    
    var shiJianLabel:UILabel?
    var shiJianLabelValue:UILabel?
    
    var suDuLabel:UILabel?
    var suDuLabelValue:UILabel?
    
    //定义三个UIView,用于分割成四个模块
    var leftView:UIView?
    var middleView:UIView?
    var rightView:UIView?
    
    //定义一条横线UIView
    var secondHengXianView:UIView?
    
    //定义一个变量,用于存储正确数
    var zhengQueShu:Int = 0

    //创建两个UILabel对象,用于存储从文章.xml文件中读取的汉字
    var hanZiFromXml:UILabel?
    var behindHanZiFromXml:UILabel?
    
    //创建一个String类型的字符串,用于存储从文章.xml文件中读取的汉字
    var readedHanZiFromXML:String?
    
    //定义一个UITextView做键盘
    var jianPanTextView:UITextView?
    
    //定义两个数组,分别用于保存遍历字符串拼接,得到的数组
    var firstRowHanZi:String = ""
    var secondRowHanZi:String = ""
    
    //定义两个数组,分别用于保存使用,切割得到的数组
    var firstShiYongDouHaoQieGeHanZiArray = [String]()
    var secondShiYongDouHaoQieGeHanZiArray = [String]()
    
    //定义一个指针,指向文章的开头处
    var pointer:Int = 0
    var behindPointer:Int = 0
    
    //定义一个字符串保存截取的字符串
    var subString:String = ""
    
    var behindSubString:String = ""
    //定义一个变量,用于存储所有整数
    var suoYouZhengShu:Int = 0
    
    //定义一个常量,用于保存每次输入完两排数字之后从哪个下标开始计算
    var congNaLiKaiShiDuConstant:Int = 0
    
    //定义一个变量,用于保存共循环多少次
    var loopNumber:Int = 0
    
    //定义一个变量,保存遍历次数
    var bianLiCiShu:Int = 0
    
    //定义一个变量,用于存储正确率
    var zhengQueLv:Double = 0.0
    
    //定义一个变量,用于存储小键盘训练速度
    var wenZhangLianXiSuDu = 0
    
    //定义一个定时器
    var timer:Timer!
    
    //定义一个变量:保存计时器时间的值
    var timerValue:Int = 0
    
    var attArr:NSMutableArray = NSMutableArray.init(capacity: 0)
    
    var rangeArray = NSMutableArray.init(capacity: 0)
    
    var attArrayBehind:NSMutableArray = NSMutableArray.init(capacity: 0)
    
    var rangeArrayBehind:NSMutableArray = NSMutableArray.init(capacity: 0)
    
    //定义一个变量,用于保存输入的字符的个数
    //var tempNumber = 0
    
    override func viewDidLoad() {
        fengZhuangViewDidLoad()
    }

    func fengZhuangViewDidLoad(){
       
        //设置viewController上面的标题
        if let moShiLeiBie = moShiLeiBie{
            self.title = "\(moShiLeiBie)模式"
            self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont(name: "STHeitiSC-Medium", size: 20.0)!]
            //修改导航栏按钮颜色
            self.navigationController?.navigationBar.tintColor = UIColor.white
            self.navigationController?.navigationBar.barTintColor =
                UIColor(red: 44/255, green: 62/255, blue: 80/255, alpha: 1)
        }
        
        self.view.backgroundColor = UIColor(red: 232/255.0,green:232/255.0,blue:232/255.0,alpha:1)
        let leftBarButton = UIButton(frame:CGRect(x: 0,y: 0,width: 20,height: 20))
        leftBarButton.setBackgroundImage(UIImage(named: "fanhui"), for: UIControlState())
        leftBarButton.addTarget(self, action: #selector(backToPrevious), for: .touchUpInside)
        let leftBarButtonItem = UIBarButtonItem(customView: leftBarButton)
        self.navigationItem.leftBarButtonItems = [leftBarButtonItem]

        
        //初始化scrollView
        scrollView = UIScrollView(frame:CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
        scrollView?.backgroundColor = UIColor.white
        scrollView?.contentSize = CGSize(width: screenWidth, height: screenHeight)
        scrollView?.indicatorStyle = UIScrollViewIndicatorStyle.white
        scrollView?.showsVerticalScrollIndicator = true
        scrollView?.showsHorizontalScrollIndicator = false
        
        
        //初始化三个imageView
        zanTingImageView = UIImageView(frame:CGRect(x:(screenWidth/3-30)/2,y:5,width:30,height:30))
        zanTingImageView!.contentMode = .scaleAspectFit
        let zanTingImage = UIImage(named: "zanting")
        zanTingImageView!.image = zanTingImage
        
        chaKanJieGuoImageView = UIImageView(frame:CGRect(x:(screenWidth/3)+(screenWidth/3-30)/2,y:5,width:30,height:30))
        chaKanJieGuoImageView!.contentMode = .scaleAspectFit
        let chaKanChengJiImage = UIImage(named: "chakanchengji")
        chaKanJieGuoImageView!.image = chaKanChengJiImage
        
        jieShuImageView = UIImageView(frame:CGRect(x:(screenWidth/3)*2+(screenWidth/3-30)/2,y:5,width:30,height:30))
        jieShuImageView!.contentMode = .scaleAspectFit
        let jieShuImage = UIImage(named: "jieshu")
        jieShuImageView!.image = jieShuImage
        
        //初始化两个View对象
        firstFenGeXianView = UIView(frame:CGRect(x:screenWidth/3,y:3,width:1,height:60))
        firstFenGeXianView!.backgroundColor = UIColor(red: 200/255,green: 199/255,blue: 204/255,alpha: 1)
        
        secondFenGeXianView = UIView(frame:CGRect(x:(screenWidth/3)*2,y:3,width:1,height:60))
        secondFenGeXianView!.backgroundColor = UIColor(red: 200/255,green: 199/255,blue: 204/255,alpha: 1)
        
        //初始化三个按钮
        zanTingBtn = UIButton(frame:CGRect(x: (screenWidth/3-80)/2,y: 5+30+5,width: 80,height: 20))
        
        zanTingBtn?.setTitle("暂停", for: UIControlState())
        zanTingBtn?.setTitleColor(UIColor.black, for: UIControlState())
        zanTingBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        chaKanJieGuoBtn = UIButton(frame:CGRect(x: (screenWidth/3)+(screenWidth/3-80)/2,y: 5+30+5,width: 80,height: 20))
        
        chaKanJieGuoBtn?.setTitle("查看结果", for: UIControlState())
        chaKanJieGuoBtn?.setTitleColor(UIColor.black, for: UIControlState())
        chaKanJieGuoBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        chaKanJieGuoBtn?.isUserInteractionEnabled = false
        
        jieShuBtn = UIButton(frame:CGRect(x: (screenWidth/3)*2+(screenWidth/3-80)/2,y: 5+30+5,width: 80,height: 20))
        
        jieShuBtn?.setTitle("结束", for: UIControlState())
        
        jieShuBtn?.setTitleColor(UIColor.black, for: UIControlState())
        jieShuBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        //初始化横线UIView
        hengXianView = UIView(frame:CGRect(x: 10,y: 67,width: screenWidth-10,height: 1))
        hengXianView?.backgroundColor = UIColor(red: 200/255,green: 199/255,blue: 204/255,alpha: 1)
        
        //初始化8个UIlabel对象
        //初始化第一个UIlabel对象
        zhengQueShuLabel = UILabel(frame:CGRect(x: (screenWidth/4-60)/2,y: 70,width: 60,height: 20))
        fengZhuangLabelAttributes(zhengQueShuLabel!)
        
        
        //初始化第二个UIlabel对象
        zhengQueShuLabelValue = UILabel(frame:CGRect(x: (screenWidth/4-60)/2,y: 70+20+5,width: 60,height: 20))
        fengZhuangLabelAttributes(zhengQueShuLabelValue!)
        
        //初始化第三个UILabel对象
        zhengQueLvLabel = UILabel(frame:CGRect(x: screenWidth/4+(screenWidth/4-60)/2,y: 70,width: 60,height: 20))
        fengZhuangLabelAttributes(zhengQueLvLabel!)
        
        //初始化第四个UILabel对象
        zhengQueLvLabelValue = UILabel(frame:CGRect(x: screenWidth/4+(screenWidth/4-60)/2,y: 70+20+5,width: 60,height: 20))
        fengZhuangLabelAttributes(zhengQueLvLabelValue!)
        
        //初始化第五个UIlabel对象
        shiJianLabel = UILabel(frame:CGRect(x: screenWidth/2+(screenWidth/4-60)/2,y: 70,width: 60,height: 20))
        fengZhuangLabelAttributes(shiJianLabel!)
        
        //初始化第六个UIlabel对象
        shiJianLabelValue = UILabel(frame:CGRect(x: screenWidth/2+(screenWidth/4-60)/2,y: 70+20+5,width: 60,height: 20))
        fengZhuangLabelAttributes(shiJianLabelValue!)
        
        //初始化第七个UIlabel对象
        suDuLabel = UILabel(frame:CGRect(x: (screenWidth/4)*3+(screenWidth/4-60)/2,y: 70,width: 60,height: 20))
        fengZhuangLabelAttributes(suDuLabel!)
        
        //初始化第八个UIlabel对象
        suDuLabelValue = UILabel(frame:CGRect(x: (screenWidth/4)*3+(screenWidth/4-60)/2,y: 70+20+5,width: 60,height: 20))
        fengZhuangLabelAttributes(suDuLabelValue!)
        
        //初始化leftView
        leftView = UIView(frame:CGRect(x:screenWidth/4,y:70,width:1,height:50))
        leftView?.backgroundColor = UIColor(red: 200/255,green: 199/255,blue: 204/255,alpha: 1)
        
        //初始化middleView
        middleView = UIView(frame:CGRect(x: screenWidth/2,y:70,width:1,height:50))
        middleView?.backgroundColor = UIColor(red: 200/255,green: 199/255,blue: 204/255,alpha: 1)
        
        //初始化rightView
        rightView = UIView(frame:CGRect(x: (screenWidth/4)*3,y:70,width:1,height:50))
        rightView?.backgroundColor = UIColor(red: 200/255,green: 199/255,blue: 204/255,alpha: 1)
        
        //初始化第二条横线View
        secondHengXianView = UIView(frame:CGRect(x: 10,y: 70+50+2,width: screenWidth-10,height: 0.5))
        secondHengXianView?.backgroundColor = UIColor(red: 200/255,green: 199/255,blue: 204/255,alpha: 1)
        
        //初始化第八个UIlabel对象
        hanZiFromXml = UILabel(frame:CGRect(x: 10,y: 70+50+10,width: screenWidth-20,height: 60))
        fengZhuangLabelAttributes(hanZiFromXml!)
        
        behindHanZiFromXml = UILabel(frame:CGRect(x: 10,y: 70+50+10+60+2,width: screenWidth-20,height: 60))
        fengZhuangLabelAttributes(behindHanZiFromXml!)
        
        jianPanTextView = UITextView(frame:CGRect(x: 10,y: 70+50+10+60+2,width: screenWidth-20,height: 0))
        jianPanTextView?.becomeFirstResponder()
        jianPanTextView?.delegate = self
        
        if let moShiCategory = moShiLeiBie{
            print(moShiCategory)
            if moShiCategory == "计时"{
                timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(genJuSelectedValueJiShi), userInfo: nil, repeats: true)
            }else if moShiCategory == "定时"{
                //将传递过来的分钟值转换为秒数
                timerValue = 60 * Int(fenZhongZhi!)!
                
                timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(genJuXuanZhongFenZhongZhiDaoJiShi), userInfo: nil, repeats: true)
            }
        }

        //分别给三个按钮添加点击事件
        zanTingBtn!.addTarget(self,action:#selector(zanTing),for:.touchUpInside)
       chaKanJieGuoBtn!.addTarget(self,action:#selector(chaKanJieGuo),for:.touchUpInside)
        jieShuBtn!.addTarget(self,action:#selector(jieShu),for:.touchUpInside)
        
        scrollView?.addSubview(firstFenGeXianView!)
        scrollView?.addSubview(secondFenGeXianView!)
        scrollView?.addSubview(zanTingBtn!)
        scrollView?.addSubview(chaKanJieGuoBtn!)
        scrollView?.addSubview(jieShuBtn!)
        scrollView?.addSubview(hengXianView!)
        scrollView?.addSubview(zanTingImageView!)
        scrollView?.addSubview(chaKanJieGuoImageView!)
        scrollView?.addSubview(jieShuImageView!)
        scrollView?.addSubview(zhengQueShuLabel!)
        scrollView?.addSubview(zhengQueLvLabel!)
        scrollView?.addSubview(shiJianLabel!)
        scrollView?.addSubview(suDuLabel!)
        scrollView?.addSubview(zhengQueShuLabelValue!)
        scrollView?.addSubview(zhengQueLvLabelValue!)
        scrollView?.addSubview(shiJianLabelValue!)
        scrollView?.addSubview(suDuLabelValue!)
        scrollView?.addSubview(leftView!)
        scrollView?.addSubview(middleView!)
        scrollView?.addSubview(rightView!)
        scrollView?.addSubview(secondHengXianView!)
        scrollView?.addSubview(hanZiFromXml!)
        scrollView?.addSubview(behindHanZiFromXml!)
        scrollView?.addSubview(jianPanTextView!)
        self.view.addSubview(scrollView!)
        print(wenZhangMoShi as Any)
        if wenZhangMoShi == "苏州"{
            jieXiSuZhouXmlWenJian()
        }else if wenZhangMoShi == "中国画"{
            jieXiZhongGuoHuaXmlWenJian()
        }else if wenZhangMoShi == "火-巴金"{
            jieXiHuoBaJinXmlWenJian()
        }else if wenZhangMoShi == "奥运会五环标志"{
            jieXiAoYunHuiWuHuanBiaoZhiXmlWenJian()
        }else if wenZhangMoShi == "云水苍茫说三山"{
            jieXiYunShuiCangMangShuoSanShanXmlWenJian()
        }else if wenZhangMoShi == "全民健身计划纲要"{
            jieXiQuanMinJianShenJiHuaGangYaoXmlWenJian()
        }

        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        if let moShiLeiBie = moShiLeiBie{
            self.title = "\(moShiLeiBie)模式"
        }
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont(name: "STHeitiSC-Medium", size: 20.0)!]
    }

    
    
    /*
     作者:张振东
     时间:2016-10-03
     功能:实现从文章xml文件中读取xml文件,并保存为String[]
     */
    func jieXiSuZhouXmlWenJian(){
        let file = Bundle.main.path(forResource: "苏州", ofType: "xml")
        //获取xml文件路径
        let url = URL(fileURLWithPath: file!)
        //获取xml文件内容
        let xmlData = try! Data(contentsOf: url)
        //使用NSData对象初始化文档对象
        //这里的语法已经把OC的初始化函数直接转换过来了
        let doc:GDataXMLDocument = try! GDataXMLDocument(data:xmlData, options : 0)
        
        //通过XPath方式获取NewDataSet节点下的所有Table节点，在路径复杂时特别方便
        let string = try! doc.nodes(forXPath: "//string", namespaces: nil) as! [GDataXMLElement]
        
        readedHanZiFromXML = string[0].stringValue()
        print(readedHanZiFromXML as Any)
        
        loopNumber = Int((readedHanZiFromXML?.characters.count)!/20)+1
        
        for index in 0...1{
            if index == 0{
                subString = (readedHanZiFromXML! as NSString).substring(with: NSMakeRange(congNaLiKaiShiDuConstant, 10))//苏州是全国重点旅游城
                print(subString)
                hanZiFromXml?.text = "\(subString)"
                for everyChar in subString.characters{
                    print(everyChar)
                    if firstRowHanZi.characters.count != 18{
                        firstRowHanZi = firstRowHanZi + "\(everyChar)"
                        firstRowHanZi = firstRowHanZi + ","
                        
                    }else {
                        firstRowHanZi = firstRowHanZi + "\(everyChar)"
                    }
                }
            firstShiYongDouHaoQieGeHanZiArray = (firstRowHanZi.components(separatedBy: ","))
            print(firstShiYongDouHaoQieGeHanZiArray)
                
            }else if index == 1{
                let startIndex1:String.Index = (readedHanZiFromXML?.startIndex)!
                let offsetIndex:String.Index = (readedHanZiFromXML?.index(startIndex1, offsetBy: 10))!
                let offsetRange = startIndex1 ..< offsetIndex
                //let startIndex = readedHanZiFromXML?.startIndex
                //let endIndex =  <#T##String.CharacterView corresponding to your index##String.CharacterView#>.index(startIndex!, offsetBy: 9)
                //let endIndex = readedHanZiFromXML.index
                //let nsRange = startIndex!...endIndex
                //readedHanZiFromXML!.replaceSubrange(nsRange, with: "")
                readedHanZiFromXML?.replaceSubrange(offsetRange, with: "")
                behindSubString = (readedHanZiFromXML! as NSString).substring(with: NSMakeRange(congNaLiKaiShiDuConstant, 10))
                print(behindSubString)
                behindHanZiFromXml?.text = "\(behindSubString)"
                
                for everyChar in behindSubString.characters{
                    print(everyChar)
                    if secondRowHanZi.characters.count != 18{
                        secondRowHanZi = secondRowHanZi + "\(everyChar)"
                        secondRowHanZi = secondRowHanZi + ","
                    }else{
                        secondRowHanZi = secondRowHanZi + "\(everyChar)"
                    }
                }
                secondShiYongDouHaoQieGeHanZiArray = (secondRowHanZi.components(separatedBy: ","))
                print(secondShiYongDouHaoQieGeHanZiArray)
                
            }
        }
    }

    /*
     作者:张振东
     时间:2016-10-04
     功能:实现从中国画.xml文件中读取xml文件,并保存为String[]
     */
    func jieXiZhongGuoHuaXmlWenJian(){
        let file = Bundle.main.path(forResource: "中国画", ofType: "xml")
        //获取xml文件路径
        let url = URL(fileURLWithPath: file!)
        //获取xml文件内容
        let xmlData = try! Data(contentsOf: url)
        //使用NSData对象初始化文档对象
        //这里的语法已经把OC的初始化函数直接转换过来了
        let doc:GDataXMLDocument = try! GDataXMLDocument(data:xmlData, options : 0)
        
        //通过XPath方式获取NewDataSet节点下的所有Table节点，在路径复杂时特别方便
        let string = try! doc.nodes(forXPath: "//string", namespaces: nil) as! [GDataXMLElement]
        
        readedHanZiFromXML = string[0].stringValue()
        print(readedHanZiFromXML as Any)
        
        loopNumber = Int((readedHanZiFromXML?.characters.count)!/20)+1
        
        for index in 0...1{
            if index == 0{
                subString = (readedHanZiFromXML! as NSString).substring(with: NSMakeRange(congNaLiKaiShiDuConstant, 10))//苏州是全国重点旅游城
                print(subString)
                hanZiFromXml?.text = "\(subString)"
                for everyChar in subString.characters{
                    print(everyChar)
                    if firstRowHanZi.characters.count != 18{
                        firstRowHanZi = firstRowHanZi + "\(everyChar)"
                        firstRowHanZi = firstRowHanZi + ","
                        
                    }else {
                        firstRowHanZi = firstRowHanZi + "\(everyChar)"
                    }
                }
                firstShiYongDouHaoQieGeHanZiArray = (firstRowHanZi.components(separatedBy: ","))
                print(firstShiYongDouHaoQieGeHanZiArray)
                
            }else if index == 1{
                //let startIndex = readedHanZiFromXML?.startIndex
                //let endIndex =  <#T##String.CharacterView corresponding to your index##String.CharacterView#>.index(startIndex!, offsetBy: 9)
                //let nsRange = startIndex!...endIndex
                //readedHanZiFromXML!.replaceSubrange(nsRange, with: "")
                
                let startIndex1:String.Index = (readedHanZiFromXML?.startIndex)!
                let offsetIndex:String.Index = (readedHanZiFromXML?.index(startIndex1, offsetBy: 9))!
                let offsetRange = startIndex1 ..< offsetIndex
                //let startIndex = readedHanZiFromXML?.startIndex
                //let endIndex =  <#T##String.CharacterView corresponding to your index##String.CharacterView#>.index(startIndex!, offsetBy: 9)
                //let endIndex = readedHanZiFromXML.index
                //let nsRange = startIndex!...endIndex
                //readedHanZiFromXML!.replaceSubrange(nsRange, with: "")
                readedHanZiFromXML?.replaceSubrange(offsetRange, with: "")
                behindSubString = (readedHanZiFromXML! as NSString).substring(with: NSMakeRange(congNaLiKaiShiDuConstant, 10))
                print(behindSubString)
                behindHanZiFromXml?.text = "\(behindSubString)"
                
                for everyChar in behindSubString.characters{
                    print(everyChar)
                    if secondRowHanZi.characters.count != 18{
                        secondRowHanZi = secondRowHanZi + "\(everyChar)"
                        secondRowHanZi = secondRowHanZi + ","
                    }else{
                        secondRowHanZi = secondRowHanZi + "\(everyChar)"
                    }
                }
                secondShiYongDouHaoQieGeHanZiArray = (secondRowHanZi.components(separatedBy: ","))
                print(secondShiYongDouHaoQieGeHanZiArray)
                
            }
        }

    }
    
    /*
     作者:张振东
     时间:2016-10-04
     功能:实现从火-巴金.xml文件中读取xml文件,并保存为String[]
     */
    func jieXiHuoBaJinXmlWenJian(){
        let file = Bundle.main.path(forResource: "火-巴金", ofType: "xml")
        //获取xml文件路径
        let url = URL(fileURLWithPath: file!)
        //获取xml文件内容
        let xmlData = try! Data(contentsOf: url)
        //使用NSData对象初始化文档对象
        //这里的语法已经把OC的初始化函数直接转换过来了
        let doc:GDataXMLDocument = try! GDataXMLDocument(data:xmlData, options : 0)
        
        //通过XPath方式获取NewDataSet节点下的所有Table节点，在路径复杂时特别方便
        let string = try! doc.nodes(forXPath: "//string", namespaces: nil) as! [GDataXMLElement]
        
        readedHanZiFromXML = string[0].stringValue()
        print(readedHanZiFromXML as Any)
        
        loopNumber = Int((readedHanZiFromXML?.characters.count)!/20)+1
        
        for index in 0...1{
            if index == 0{
                subString = (readedHanZiFromXML! as NSString).substring(with: NSMakeRange(congNaLiKaiShiDuConstant, 10))//苏州是全国重点旅游城
                print(subString)
                hanZiFromXml?.text = "\(subString)"
                for everyChar in subString.characters{
                    print(everyChar)
                    if firstRowHanZi.characters.count != 18{
                        firstRowHanZi = firstRowHanZi + "\(everyChar)"
                        firstRowHanZi = firstRowHanZi + ","
                        
                    }else {
                        firstRowHanZi = firstRowHanZi + "\(everyChar)"
                    }
                }
                firstShiYongDouHaoQieGeHanZiArray = (firstRowHanZi.components(separatedBy: ","))
                print(firstShiYongDouHaoQieGeHanZiArray)
                
            }else if index == 1{
                //let startIndex = readedHanZiFromXML?.startIndex
                //let endIndex =  <#T##String.CharacterView corresponding to your index##String.CharacterView#>.index(startIndex!, offsetBy: 9)
                //let nsRange = startIndex!...endIndex
                //readedHanZiFromXML!.replaceSubrange(nsRange, with: "")
                
                let startIndex1:String.Index = (readedHanZiFromXML?.startIndex)!
                let offsetIndex:String.Index = (readedHanZiFromXML?.index(startIndex1, offsetBy: 9))!
                let offsetRange = startIndex1 ..< offsetIndex
                //let startIndex = readedHanZiFromXML?.startIndex
                //let endIndex =  <#T##String.CharacterView corresponding to your index##String.CharacterView#>.index(startIndex!, offsetBy: 9)
                //let endIndex = readedHanZiFromXML.index
                //let nsRange = startIndex!...endIndex
                //readedHanZiFromXML!.replaceSubrange(nsRange, with: "")
                readedHanZiFromXML?.replaceSubrange(offsetRange, with: "")
                behindSubString = (readedHanZiFromXML! as NSString).substring(with: NSMakeRange(congNaLiKaiShiDuConstant, 10))
                print(behindSubString)
                behindHanZiFromXml?.text = "\(behindSubString)"
                
                for everyChar in behindSubString.characters{
                    print(everyChar)
                    if secondRowHanZi.characters.count != 18{
                        secondRowHanZi = secondRowHanZi + "\(everyChar)"
                        secondRowHanZi = secondRowHanZi + ","
                    }else{
                        secondRowHanZi = secondRowHanZi + "\(everyChar)"
                    }
                }
                secondShiYongDouHaoQieGeHanZiArray = (secondRowHanZi.components(separatedBy: ","))
                print(secondShiYongDouHaoQieGeHanZiArray)
            }
        }
    }
    
    /*
     作者:张振东
     时间:2016-10-04
     功能:实现从奥运会五环.xml文件中读取xml文件,并保存为String[]
     */
    func jieXiAoYunHuiWuHuanXmlWenJian(){
        let file = Bundle.main.path(forResource: "奥运会五环", ofType: "xml")
        //获取xml文件路径
        let url = URL(fileURLWithPath: file!)
        //获取xml文件内容
        let xmlData = try! Data(contentsOf: url)
        //使用NSData对象初始化文档对象
        //这里的语法已经把OC的初始化函数直接转换过来了
        let doc:GDataXMLDocument = try! GDataXMLDocument(data:xmlData, options : 0)
        
        //通过XPath方式获取NewDataSet节点下的所有Table节点，在路径复杂时特别方便
        let string = try! doc.nodes(forXPath: "//string", namespaces: nil) as! [GDataXMLElement]
        
        readedHanZiFromXML = string[0].stringValue()
        print(readedHanZiFromXML as Any)
        
        loopNumber = Int((readedHanZiFromXML?.characters.count)!/20)+1
        
        for index in 0...1{
            if index == 0{
                subString = (readedHanZiFromXML! as NSString).substring(with: NSMakeRange(congNaLiKaiShiDuConstant, 10))//苏州是全国重点旅游城
                print(subString)
                hanZiFromXml?.text = "\(subString)"
                for everyChar in subString.characters{
                    print(everyChar)
                    if firstRowHanZi.characters.count != 18{
                        firstRowHanZi = firstRowHanZi + "\(everyChar)"
                        firstRowHanZi = firstRowHanZi + ","
                        
                    }else {
                        firstRowHanZi = firstRowHanZi + "\(everyChar)"
                    }
                }
                firstShiYongDouHaoQieGeHanZiArray = (firstRowHanZi.components(separatedBy: ","))
                print(firstShiYongDouHaoQieGeHanZiArray)
                
            }else if index == 1{
                //let startIndex = readedHanZiFromXML?.startIndex
                //let endIndex =  <#T##String.CharacterView corresponding to your index##String.CharacterView#>.index(startIndex!, offsetBy: 9)
                //let nsRange = startIndex!...endIndex
                //readedHanZiFromXML!.replaceSubrange(nsRange, with: "")
                
                
                let startIndex1:String.Index = (readedHanZiFromXML?.startIndex)!
                let offsetIndex:String.Index = (readedHanZiFromXML?.index(startIndex1, offsetBy: 9))!
                let offsetRange = startIndex1 ..< offsetIndex
                //let startIndex = readedHanZiFromXML?.startIndex
                //let endIndex =  <#T##String.CharacterView corresponding to your index##String.CharacterView#>.index(startIndex!, offsetBy: 9)
                //let endIndex = readedHanZiFromXML.index
                //let nsRange = startIndex!...endIndex
                //readedHanZiFromXML!.replaceSubrange(nsRange, with: "")
                readedHanZiFromXML?.replaceSubrange(offsetRange, with: "")
                behindSubString = (readedHanZiFromXML! as NSString).substring(with: NSMakeRange(congNaLiKaiShiDuConstant, 10))
                print(behindSubString)
                behindHanZiFromXml?.text = "\(behindSubString)"
                
                for everyChar in behindSubString.characters{
                    print(everyChar)
                    if secondRowHanZi.characters.count != 18{
                        secondRowHanZi = secondRowHanZi + "\(everyChar)"
                        secondRowHanZi = secondRowHanZi + ","
                    }else{
                        secondRowHanZi = secondRowHanZi + "\(everyChar)"
                    }
                }
                secondShiYongDouHaoQieGeHanZiArray = (secondRowHanZi.components(separatedBy: ","))
                print(secondShiYongDouHaoQieGeHanZiArray)
                
            }
        }

    }
    
    /*
     作者:张振东
     时间:2016-10-04
     功能:实现从奥运会五环标志.xml文件中读取xml文件,并保存为String[]
     */
    func jieXiAoYunHuiWuHuanBiaoZhiXmlWenJian(){
        let file = Bundle.main.path(forResource: "奥运会五环标志", ofType: "xml")
        //获取xml文件路径
        let url = URL(fileURLWithPath: file!)
        //获取xml文件内容
        let xmlData = try! Data(contentsOf: url)
        //使用NSData对象初始化文档对象
        //这里的语法已经把OC的初始化函数直接转换过来了
        let doc:GDataXMLDocument = try! GDataXMLDocument(data:xmlData, options : 0)
        
        //通过XPath方式获取NewDataSet节点下的所有Table节点，在路径复杂时特别方便
        let string = try! doc.nodes(forXPath: "//string", namespaces: nil) as! [GDataXMLElement]
        
        readedHanZiFromXML = string[0].stringValue()
        print(readedHanZiFromXML as Any)
        
        loopNumber = Int((readedHanZiFromXML?.characters.count)!/20)+1
        
        for index in 0...1{
            if index == 0{
                subString = (readedHanZiFromXML! as NSString).substring(with: NSMakeRange(congNaLiKaiShiDuConstant, 10))//苏州是全国重点旅游城
                print(subString)
                hanZiFromXml?.text = "\(subString)"
                for everyChar in subString.characters{
                    print(everyChar)
                    if firstRowHanZi.characters.count != 18{
                        firstRowHanZi = firstRowHanZi + "\(everyChar)"
                        firstRowHanZi = firstRowHanZi + ","
                        
                    }else {
                        firstRowHanZi = firstRowHanZi + "\(everyChar)"
                    }
                }
                firstShiYongDouHaoQieGeHanZiArray = (firstRowHanZi.components(separatedBy: ","))
                print(firstShiYongDouHaoQieGeHanZiArray)
                
            }else if index == 1{
                //let startIndex = readedHanZiFromXML?.startIndex
                //let endIndex =  <#T##String.CharacterView corresponding to your index##String.CharacterView#>.index(startIndex!, offsetBy: 9)
                //let nsRange = startIndex!...endIndex
                //readedHanZiFromXML!.replaceSubrange(nsRange, with: "")
                
                let startIndex1:String.Index = (readedHanZiFromXML?.startIndex)!
                let offsetIndex:String.Index = (readedHanZiFromXML?.index(startIndex1, offsetBy: 9))!
                let offsetRange = startIndex1 ..< offsetIndex
                //let startIndex = readedHanZiFromXML?.startIndex
                //let endIndex =  <#T##String.CharacterView corresponding to your index##String.CharacterView#>.index(startIndex!, offsetBy: 9)
                //let endIndex = readedHanZiFromXML.index
                //let nsRange = startIndex!...endIndex
                //readedHanZiFromXML!.replaceSubrange(nsRange, with: "")
                readedHanZiFromXML?.replaceSubrange(offsetRange, with: "")
                behindSubString = (readedHanZiFromXML! as NSString).substring(with: NSMakeRange(congNaLiKaiShiDuConstant, 10))
                print(behindSubString)
                behindHanZiFromXml?.text = "\(behindSubString)"
                
                for everyChar in behindSubString.characters{
                    print(everyChar)
                    if secondRowHanZi.characters.count != 18{
                        secondRowHanZi = secondRowHanZi + "\(everyChar)"
                        secondRowHanZi = secondRowHanZi + ","
                    }else{
                        secondRowHanZi = secondRowHanZi + "\(everyChar)"
                    }
                }
                secondShiYongDouHaoQieGeHanZiArray = (secondRowHanZi.components(separatedBy: ","))
                print(secondShiYongDouHaoQieGeHanZiArray)
                
            }
        }
    }
    
    /*
     作者:张振东
     时间:2016-10-04
     功能:实现从云水苍茫说三山.xml文件中读取xml文件,并保存为String[]
     */
    func jieXiYunShuiCangMangShuoSanShanXmlWenJian(){
        let file = Bundle.main.path(forResource: "云水苍茫说三山", ofType: "xml")
        //获取xml文件路径
        let url = URL(fileURLWithPath: file!)
        //获取xml文件内容
        let xmlData = try! Data(contentsOf: url)
        //使用NSData对象初始化文档对象
        //这里的语法已经把OC的初始化函数直接转换过来了
        let doc:GDataXMLDocument = try! GDataXMLDocument(data:xmlData, options : 0)
        
        //通过XPath方式获取NewDataSet节点下的所有Table节点，在路径复杂时特别方便
        let string = try! doc.nodes(forXPath: "//string", namespaces: nil) as! [GDataXMLElement]
        
        readedHanZiFromXML = string[0].stringValue()
        print(readedHanZiFromXML as Any)
        
        loopNumber = Int((readedHanZiFromXML?.characters.count)!/20)+1
        
        for index in 0...1{
            if index == 0{
                subString = (readedHanZiFromXML! as NSString).substring(with: NSMakeRange(congNaLiKaiShiDuConstant, 10))//苏州是全国重点旅游城
                print(subString)
                hanZiFromXml?.text = "\(subString)"
                for everyChar in subString.characters{
                    print(everyChar)
                    if firstRowHanZi.characters.count != 18{
                        firstRowHanZi = firstRowHanZi + "\(everyChar)"
                        firstRowHanZi = firstRowHanZi + ","
                        
                    }else {
                        firstRowHanZi = firstRowHanZi + "\(everyChar)"
                    }
                }
                firstShiYongDouHaoQieGeHanZiArray = (firstRowHanZi.components(separatedBy: ","))
                print(firstShiYongDouHaoQieGeHanZiArray)
                
            }else if index == 1{
                //let startIndex = readedHanZiFromXML?.startIndex
                //let endIndex =  <#T##String.CharacterView corresponding to your index##String.CharacterView#>.index(startIndex!, offsetBy: 9)
                //let nsRange = startIndex!...endIndex
                //readedHanZiFromXML!.replaceSubrange(nsRange, with: "")
                
                let startIndex1:String.Index = (readedHanZiFromXML?.startIndex)!
                let offsetIndex:String.Index = (readedHanZiFromXML?.index(startIndex1, offsetBy: 9))!
                let offsetRange = startIndex1 ..< offsetIndex
                //let startIndex = readedHanZiFromXML?.startIndex
                //let endIndex =  <#T##String.CharacterView corresponding to your index##String.CharacterView#>.index(startIndex!, offsetBy: 9)
                //let endIndex = readedHanZiFromXML.index
                //let nsRange = startIndex!...endIndex
                //readedHanZiFromXML!.replaceSubrange(nsRange, with: "")
                readedHanZiFromXML?.replaceSubrange(offsetRange, with: "")
                behindSubString = (readedHanZiFromXML! as NSString).substring(with: NSMakeRange(congNaLiKaiShiDuConstant, 10))
                print(behindSubString)
                behindHanZiFromXml?.text = "\(behindSubString)"
                
                for everyChar in behindSubString.characters{
                    print(everyChar)
                    if secondRowHanZi.characters.count != 18{
                        secondRowHanZi = secondRowHanZi + "\(everyChar)"
                        secondRowHanZi = secondRowHanZi + ","
                    }else{
                        secondRowHanZi = secondRowHanZi + "\(everyChar)"
                    }
                }
                secondShiYongDouHaoQieGeHanZiArray = (secondRowHanZi.components(separatedBy: ","))
                print(secondShiYongDouHaoQieGeHanZiArray)
                
            }
        }
    }
    
    /*
     作者:张振东
     时间:2016-10-04
     功能:实现从云水苍茫说三山.xml文件中读取xml文件,并保存为String[]
     */
    func jieXiQuanMinJianShenJiHuaGangYaoXmlWenJian(){
        let file = Bundle.main.path(forResource: "全民健身计划纲要", ofType: "xml")
        //获取xml文件路径
        let url = URL(fileURLWithPath: file!)
        //获取xml文件内容
        let xmlData = try! Data(contentsOf: url)
        //使用NSData对象初始化文档对象
        //这里的语法已经把OC的初始化函数直接转换过来了
        let doc:GDataXMLDocument = try! GDataXMLDocument(data:xmlData, options : 0)
        
        //通过XPath方式获取NewDataSet节点下的所有Table节点，在路径复杂时特别方便
        let string = try! doc.nodes(forXPath: "//string", namespaces: nil) as! [GDataXMLElement]
        
        readedHanZiFromXML = string[0].stringValue()
        print(readedHanZiFromXML as Any)
        
        loopNumber = Int((readedHanZiFromXML?.characters.count)!/20)+1
        
        for index in 0...1{
            if index == 0{
                subString = (readedHanZiFromXML! as NSString).substring(with: NSMakeRange(congNaLiKaiShiDuConstant, 10))//苏州是全国重点旅游城
                print(subString)
                hanZiFromXml?.text = "\(subString)"
                for everyChar in subString.characters{
                    print(everyChar)
                    if firstRowHanZi.characters.count != 18{
                        firstRowHanZi = firstRowHanZi + "\(everyChar)"
                        firstRowHanZi = firstRowHanZi + ","
                        
                    }else {
                        firstRowHanZi = firstRowHanZi + "\(everyChar)"
                    }
                }
                firstShiYongDouHaoQieGeHanZiArray = (firstRowHanZi.components(separatedBy: ","))
                print(firstShiYongDouHaoQieGeHanZiArray)
                
            }else if index == 1{
                //let startIndex = readedHanZiFromXML?.startIndex
                //let endIndex =  <#T##String.CharacterView corresponding to your index##String.CharacterView#>.index(startIndex!, offsetBy: 9)
                //let nsRange = startIndex!...endIndex
                //readedHanZiFromXML!.replaceSubrange(nsRange, with: "")
                
                let startIndex1:String.Index = (readedHanZiFromXML?.startIndex)!
                let offsetIndex:String.Index = (readedHanZiFromXML?.index(startIndex1, offsetBy: 9))!
                let offsetRange = startIndex1 ..< offsetIndex
                //let startIndex = readedHanZiFromXML?.startIndex
                //let endIndex =  <#T##String.CharacterView corresponding to your index##String.CharacterView#>.index(startIndex!, offsetBy: 9)
                //let endIndex = readedHanZiFromXML.index
                //let nsRange = startIndex!...endIndex
                //readedHanZiFromXML!.replaceSubrange(nsRange, with: "")
                readedHanZiFromXML?.replaceSubrange(offsetRange, with: "")
                behindSubString = (readedHanZiFromXML! as NSString).substring(with: NSMakeRange(congNaLiKaiShiDuConstant, 10))
                print(behindSubString)
                behindHanZiFromXml?.text = "\(behindSubString)"
                
                for everyChar in behindSubString.characters{
                    print(everyChar)
                    if secondRowHanZi.characters.count != 18{
                        secondRowHanZi = secondRowHanZi + "\(everyChar)"
                        secondRowHanZi = secondRowHanZi + ","
                    }else{
                        secondRowHanZi = secondRowHanZi + "\(everyChar)"
                    }
                }
                secondShiYongDouHaoQieGeHanZiArray = (secondRowHanZi.components(separatedBy: ","))
                print(secondShiYongDouHaoQieGeHanZiArray)
                
            }
        }
    }
    
    /*
     作者:张振东
     时间:2016-10-03
     功能:每行字显示10个汉字
     */
    func jieQuZiFuChuan(_ string:String) -> String{
        let newSubstring = (string as NSString).substring(with: NSMakeRange(0, 10))
        return newSubstring
    }
    
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if text == "" {
            return false
        }
        return true
    }
    
    func textViewDidChange(_ textView: UITextView) {
        
        print(textView.text)
        //获得输入的字符串
        var inputTextValue = textView.text
        //获得输入的字符串的长度
        let inputTextValueCount = inputTextValue?.characters.count
        print(inputTextValueCount as Any)
        //tempNumber = inputTextValueCount!
        if bianLiCiShu < loopNumber{
            //所有数要加上去
            suoYouZhengShu = inputTextValueCount! + 20*bianLiCiShu
            if (inputTextValueCount! % 20 > 0) && (inputTextValueCount! % 20 <= 10) {
                let startIndex:String.Index? = inputTextValue?.index((inputTextValue!.startIndex), offsetBy: pointer)
                print(startIndex as Any)
                //let endIndex = <#T##String.CharacterView corresponding to `startIndex`##String.CharacterView#>.index(startIndex!, offsetBy: inputTextValueCount!-pointer)
                let endIndex1:String.Index? = (inputTextValue?.index(startIndex!, offsetBy: inputTextValueCount!-pointer))!
                
                print(endIndex1 as Any)
                let stringRange = startIndex!..<endIndex1!
                let inputSubString = inputTextValue?.substring(with: stringRange)
                print(stringRange)
                if inputSubString == (subString as NSString).substring(with: NSMakeRange(pointer, inputTextValueCount!-pointer)){
                    //字体变为绿色
                    let greenColor:NSMutableAttributedString = NSMutableAttributedString(string: (subString as NSString).substring(with: NSMakeRange(0, 10)))
                    let range1:NSRange = (subString as NSString).range(of: "\(inputSubString!)")
                    
                    attArr.add(UIColor.green)
                    rangeArray.add(range1)
                    
                    for i in 0..<rangeArray.count {
                        greenColor.addAttribute(NSForegroundColorAttributeName, value: attArr[i] as!UIColor , range: rangeArray[i] as! NSRange)
                    }
                    
                    hanZiFromXml?.attributedText = greenColor
                    //正确数+1
                    zhengQueShu = zhengQueShu + (inputSubString?.characters.count)!
                    print(zhengQueShu)
                    zhengQueShuLabelValue?.text = "\(zhengQueShu)"
                    pointer = inputTextValueCount!
                    jianPanTextView?.becomeFirstResponder()
                }else{
                    //字体变为红色
                    let redColor:NSMutableAttributedString = NSMutableAttributedString(string: (subString as NSString).substring(with: NSMakeRange(0, 10)))
                
                    let range1 = NSMakeRange(pointer,inputTextValueCount!-pointer)
                    print(range1)
                    attArr.add(UIColor.red)
                    rangeArray.add(range1)
                    
                    for i in 0 ..< rangeArray.count {
                        redColor.addAttribute(NSForegroundColorAttributeName, value: attArr[i] as! UIColor, range: rangeArray[i] as! NSRange)
                    }
                    hanZiFromXml?.attributedText = redColor
                    pointer = pointer+(inputSubString?.characters.count)!
                    jianPanTextView?.becomeFirstResponder()
                }
                
            }else if (inputTextValueCount! % 20 > 10) && (inputTextValueCount! % 21 < 21){
                //let startIndex1 = intputTextValue!.startIndex
                //let endIndex1 =  <#T##String.CharacterView corresponding to your index##String.CharacterView#>.index(startIndex1!, offsetBy: 9)
                //let nsRange1 = startIndex1!...endIndex1
                //inputTextValue!.replaceSubrange(nsRange1, with: "")
                //-=============
                let startIndex1:String.Index? = (inputTextValue!.startIndex)
                let endIndex1:String.Index? = (inputTextValue!.index(startIndex1!, offsetBy: 9))
                let nsRange1 = startIndex1! ... endIndex1!
                inputTextValue!.replaceSubrange(nsRange1, with: "")
                
                let startIndex:String.Index = (inputTextValue!.index((inputTextValue!.startIndex), offsetBy: behindPointer))
                //let endIndex = <#T##String.CharacterView corresponding to `startIndex`##String.CharacterView#>.index(startIndex!, offsetBy: (intputTextValue!.characters.count)!-behindPointer)
                let offsetIndex:String.Index = inputTextValue!.index(startIndex, offsetBy: inputTextValueCount!-pointer-behindPointer)
                
                let stringRange = startIndex ..< offsetIndex
                let inputSubString = inputTextValue?.substring(with: stringRange)
                print(stringRange)
                if inputSubString == (behindSubString as NSString).substring(with: NSMakeRange(behindPointer, (inputTextValue?.characters.count)!-behindPointer)){
                    //字体变为绿色
                    let greenColor:NSMutableAttributedString = NSMutableAttributedString(string: (behindSubString as NSString).substring(with: NSMakeRange(0, 10)))
                    let range1:NSRange = NSMakeRange(behindPointer, inputTextValue!.characters.count-behindPointer)
                    attArrayBehind.add(UIColor.green)
                    rangeArrayBehind.add(range1)
                    
                    for i in 0 ..< rangeArrayBehind.count {
                        greenColor.addAttribute(NSForegroundColorAttributeName, value: attArrayBehind[i] as! UIColor, range: rangeArrayBehind[i] as! NSRange)
                    }
                    
                    behindHanZiFromXml?.attributedText = greenColor
                    //正确数+1
                    zhengQueShu = zhengQueShu + (inputSubString?.characters.count)!
                    print(zhengQueShu)
                    zhengQueShuLabelValue?.text = "\(zhengQueShu)"
                    behindPointer = (inputTextValue!.characters.count)
                    jianPanTextView?.becomeFirstResponder()
                }else{
                    //字体变为红色
                    let redColor:NSMutableAttributedString = NSMutableAttributedString(string: (behindSubString as NSString).substring(with: NSMakeRange(0, 10)))
                    let range1 = NSMakeRange(behindPointer,(inputTextValue?.characters.count)!-behindPointer)
                    print(range1)
                    attArrayBehind.add(UIColor.red)
                    rangeArrayBehind.add(range1)
                    
                    for i in 0 ..< rangeArrayBehind.count {
                        redColor.addAttribute(NSForegroundColorAttributeName, value: attArrayBehind[i] as! UIColor, range: rangeArrayBehind[i] as! NSRange)
                    }
                    behindHanZiFromXml?.attributedText = redColor
                    behindPointer = behindPointer+(inputSubString?.characters.count)!
                    jianPanTextView?.becomeFirstResponder()
                }
            }else if inputTextValueCount! % 20 == 0{
                congNaLiKaiShiDuConstant = congNaLiKaiShiDuConstant + 20
                textView.text = ""
                inputTextValue = ""
                attArr = []
                rangeArray = []
                attArrayBehind = []
                rangeArrayBehind = []
                pointer = 0
                behindPointer = 0
                bianLiCiShu = bianLiCiShu + 1
                
                //表示输入的汉字的个数已经超出了每次显示的20个,这就需要重新生成汉字
                if wenZhangMoShi == "苏州"{
                    jieXiSuZhouXmlWenJian()
                }else if wenZhangMoShi == "中国画"{
                    jieXiZhongGuoHuaXmlWenJian()
                }else if wenZhangMoShi == "火-巴金"{
                    jieXiHuoBaJinXmlWenJian()
                }else if wenZhangMoShi == "奥运会五环"{
                    jieXiAoYunHuiWuHuanXmlWenJian()
                }else if wenZhangMoShi == "奥运会五环标志"{
                    jieXiAoYunHuiWuHuanBiaoZhiXmlWenJian()
                }else if wenZhangMoShi == "全民健身计划纲要"{
                    jieXiQuanMinJianShenJiHuaGangYaoXmlWenJian()
                }
            }
        }
        
        //计算正确率并实时显示
        let ZhengQueLv = jiSuanZhengQueLv()
        zhengQueLvLabelValue?.text = "\(ZhengQueLv)%"
        
        //计算文章练习速度并实时显示
        wenZhangLianXiSuDu = jiSuanLianXiSuDu()
        suDuLabelValue?.text = "\(wenZhangLianXiSuDu)字/分钟"
    }
    
    
    /*
     作者:张振东
     日期:2016-10-01
     功能:根据选中的分钟值计时
     */
    func genJuSelectedValueJiShi(){
        timerValue += 1
        print(fenZhongZhi as Any)
        
        //获得分钟
        let fenZhongValue = timerValue/60
        //获取秒数
        let miaoValue = timerValue%60
        
        if fenZhongValue < 10 {
            if miaoValue < 10 {
                shiJianLabelValue?.text = "00:0\(fenZhongValue):0\(miaoValue)"
            }else{
                shiJianLabelValue?.text = "00:0\(fenZhongValue):\(miaoValue)"
            }
        }else{
            if miaoValue < 10{
                shiJianLabelValue?.text = "00:\(fenZhongValue):0\(miaoValue)"
            }else{
                shiJianLabelValue?.text = "00:\(fenZhongValue):\(miaoValue)"
            }
        }
        
        //当计时结束时,可以在此进行if语句中显示最终的结果
        if timerValue == Int(fenZhongZhi!)! * 60{
            //倒计时结束
            timer.invalidate()
            //键盘失去交互性
            jianPanTextView?.isUserInteractionEnabled = false
            jianPanTextView?.resignFirstResponder()
            
            //在这里加一个alertController,提示计时结束
            let alertController = UIAlertController(title: "温馨提示",
                                                    message: "亲,计时时间到!", preferredStyle: UIAlertControllerStyle.alert)
            let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.default,
                                         handler: {
                                            action in
                                            print("点击了确定")
            })
            alertController.addAction(okAction)
            self.present(alertController, animated: true, completion: nil)
    
            //当传递过来的时间消耗完毕,计时就要从0开始
            timerValue = 0
            //查看结果具有交互性
            chaKanJieGuoBtn?.isUserInteractionEnabled = true
            print("倒计时结束")
        }
    }

    /*
     作者:张振东
     日期:2016-10-01
     功能:显示倒计时时间
     */
    func genJuXuanZhongFenZhongZhiDaoJiShi(){
        print(fenZhongZhi!)
        print(timerValue)
        if timerValue <= 0{
            timerValue = 0
        }else{
            timerValue = timerValue - 1
            //获得分钟
            let minuteValue = timerValue/60
            //获得秒数
            let secondsValue = timerValue%60
            if minuteValue < 10{
                if secondsValue < 10{
                    shiJianLabelValue?.text = "00:0\(minuteValue):0\(secondsValue)"
                }else{
                    shiJianLabelValue?.text = "00:0\(minuteValue):\(secondsValue)"
                }
            }else{
                if secondsValue < 10{
                    shiJianLabelValue?.text = "00:\(minuteValue):0\(secondsValue)"
                }else{
                    shiJianLabelValue?.text = "00:\(minuteValue):\(secondsValue)"
                }
            }
            //当计时结束时,可以在此进行if语句中显示最终的结果
            if timerValue == 0{
                //表示倒计时结束
                timer.invalidate()
                //键盘失去交互性
                jianPanTextView?.isUserInteractionEnabled = false
                jianPanTextView?.resignFirstResponder()
                
                //在这里加一个alertController,提示倒计时结束
                let alertController = UIAlertController(title: "温馨提示",
                                                        message: "亲,倒计时结束!", preferredStyle: UIAlertControllerStyle.alert)
                
                let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.default,
                                             handler: {
                                                action in
                                                print("点击了确定")
                })
                
                alertController.addAction(okAction)
                self.present(alertController, animated: true, completion: nil)

                timerValue = 0
                //查看结果具有交互性
                chaKanJieGuoBtn?.isUserInteractionEnabled = true
                print("倒计时结束!!")
            }
        }
        
    }

    /*
     作者:张振东
     日期:2016-10-04
     功能:点击暂停所做的工作
     */
    func zanTing(){
        //查看结果按钮才具有交互性
        chaKanJieGuoBtn?.isUserInteractionEnabled = true
        zanTingBtn?.isSelected = !(zanTingBtn?.isSelected)!
        print(zanTingBtn?.isSelected)
        if zanTingBtn?.isSelected == true{
            //按钮失去交互性
            jianPanTextView?.isUserInteractionEnabled = false
            
            //2、将上面的正确数、正确率、时间、速度保存在本地
            UserDefaults.standard.set(zhengQueShu, forKey: "zhengQueShuInt")
            UserDefaults.standard.set(zhengQueLv, forKey: "zhengQueLv")
            UserDefaults.standard.set(timerValue, forKey: "timerValue")
            UserDefaults.standard.set(wenZhangLianXiSuDu, forKey: "wenZhangLianXiSuDu")
            
            let a = UserDefaults.standard.integer(forKey: "timerValue")
            print(a)
            UserDefaults.standard.synchronize()
            //暂停两个字修改为开始
            zanTingBtn?.setTitle("开始", for: UIControlState())
            //暂停的按钮修改为开始按钮
            let kaiShiImage = UIImage(named: "begin")
            zanTingImageView!.image = kaiShiImage
            zanTingBtn?.setTitleColor(UIColor.gray, for: UIControlState())
            //让时间失去交互性
            timer.invalidate()
        }else{
            //键盘获得交互性
            jianPanTextView?.isUserInteractionEnabled = true
            //让暂停恢复原状
            zanTingBtn?.setTitle("暂停", for: UIControlState())
            let zanTingImage = UIImage(named: "zanting")
            zanTingImageView!.image = zanTingImage
            zanTingBtn?.setTitleColor(UIColor.black, for: UIControlState())
            
            //当开始的时候,查看结果按钮是不具有交互性的
            chaKanJieGuoBtn?.isUserInteractionEnabled = false
            //计时器开始正常工作
            if let moShiCategory = moShiLeiBie{
                print(moShiCategory)
                if moShiCategory == "计时"{
                    timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(genJuSelectedValueJiShi), userInfo: nil, repeats: true)
                }else if moShiCategory == "定时"{
                    timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(genJuXuanZhongFenZhongZhiDaoJiShi), userInfo: nil, repeats: true)
                }
            }
        }
        
    }
    
    
    /*
     作者:张振东
     日期:2016-10-04
     功能:点击查看结果按钮查看结果
     */
    func chaKanJieGuo(){
        
        let danZiChaKanJieGuoViewController = DanZiChaKanJieGuoViewController()
        if let moShiCategory = moShiLeiBie{
            print(moShiCategory)
            danZiChaKanJieGuoViewController.moShiCategory = moShiCategory
            danZiChaKanJieGuoViewController.miniteValue = Int(fenZhongZhi!)!
            danZiChaKanJieGuoViewController.wenZhangMoShi = wenZhangMoShi
        }
        self.navigationController?.pushViewController(danZiChaKanJieGuoViewController, animated: true)
    }

    /*
     作者:张振东
     日期:2016-09-29
     功能:点击结束所做的工作
     */
    func jieShu(){
        //1、时间失效
        timer.invalidate()
        //键盘失去交互性
        jianPanTextView?.isUserInteractionEnabled = false
        chaKanJieGuoBtn?.isUserInteractionEnabled = true
        //3、将上面的正确数、正确率、时间、速度保存在本地
        UserDefaults.standard.set(zhengQueShu, forKey: "zhengQueShuInt")
        UserDefaults.standard.set(zhengQueLv, forKey: "zhengQueLv")
        UserDefaults.standard.set(timerValue, forKey: "timerValue")
        UserDefaults.standard.set(wenZhangLianXiSuDu, forKey: "wenZhangLianXiSuDu")
        
    }

    /*
     作者:张振东
     时间:2016-10-03
     功能:封装UIlabel对象的属性®
     */
    @discardableResult
    func fengZhuangLabelAttributes(_ label:UILabel) -> UILabel{
        
        label.font = UIFont.systemFont(ofSize: 12)
        label.textAlignment = .center
        
        switch label {
        case zhengQueShuLabel!:
            label.text = "正确数"
            break
        case zhengQueShuLabelValue!:
            label.text = "\(zhengQueShu)"
            break
        case zhengQueLvLabel!:
            label.text = "正确率"
            break
        case zhengQueLvLabelValue!:
            label.text = "0%"
            break
        case shiJianLabel!:
            label.text = "时间"
            break
        case shiJianLabelValue!:
            label.text = "00:00:00"
            break
        case suDuLabel!:
            label.text = "速度"
            break
        case suDuLabelValue!:
            label.text = "0字/分钟"
            break
        case hanZiFromXml!:
            label.font = UIFont.systemFont(ofSize: 25)
            label.backgroundColor = UIColor(red: 236/255,green: 240/255,blue: 241/255,alpha: 1)
            break
        case behindHanZiFromXml!:
            label.font = UIFont.systemFont(ofSize: 25)
            label.backgroundColor = UIColor(red: 236/255,green: 240/255,blue: 241/255,alpha: 1)
            break
        default: break
        }
        return label
    }
    
    /*
     作者:张振东
     日期:2016-10-04
     功能:计算文章训练正确率
     */
    func jiSuanZhengQueLv() -> Double{
        
        zhengQueLv = (Double(zhengQueShu)/Double(suoYouZhengShu))*100
        //保留小数点后两位
        let zhengQueLvZhi = String(format: "%.2f", zhengQueLv)
        print(zhengQueLvZhi)
        return Double(zhengQueLvZhi)!
    }
    
    
    /*
     作者:张振东
     日期:2016-10-04
     功能:计算文章训练练习速度
     */
    func jiSuanLianXiSuDu() -> Int{
        print(Int(suoYouZhengShu))
        print(Int(fenZhongZhi!)!)
        let lianXiSudu = Int(suoYouZhengShu)/Int(fenZhongZhi!)!
        print(lianXiSudu)
        return lianXiSudu
    }

    
    //返回到上一个页面
    func backToPrevious(){
        self.navigationController?.popViewController(animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
